package ch11;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class _09_QueueEx {
	static Queue<String> q = new LinkedList<String>();
	static final int MAX_SIZE = 5; // Queue size.

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String input = "";
		
		System.out.println("If you want a help, typing the help.");
		while (true) {
			System.out.print(">> ");
					
			input = sc.nextLine().trim();
			if ("".equals(input)) continue;

			if (input.equalsIgnoreCase("q")) {
				System.out.println("Quit.");
				System.exit(0);
			} else if (input.equalsIgnoreCase("help")) {
				System.out.println(" HELP: show you the help.");
				System.out.println(" Q: quit the program.");
				System.out.println(" HISTORY: show you the recent command.");
			} else if (input.equalsIgnoreCase("history")) {
				for(String tmp:q) System.out.println(tmp);					
			} else {
				save(input);
				System.out.println(input);
			} 			
		} 
	} 

	public static void save(String input) {
		if (!"".equals(input)) q.offer(input);
		if (((LinkedList<String>) q).size() > MAX_SIZE)	q.remove();
	}
} 
/*
If you want a help, typing the help.
>> 1
1
>> 2
2
>> 3
3
>> 4
4
>> 5
5
>> 6
6
>> history
2
3
4
5
6
>> help
 HELP: show you the help.
 Q: quit the program.
 HISTORY: show you the recent command.
>> q
Quit.
*/